/**
 * Created by Li&Yan on 2017/03/10.
 */
define(['app', 'service/service'], function(app) {
    app.register.controller("inquirySheet", ['$scope', '$state', 'commonService', '$location', 'OEMIDTRUE', function($scope, $state, commonService, $location, OEMIDTRUE) {
        commonService.classify(function(data) {
            if (data.code == 0) {
                $scope.classify = data.data;
                $scope.current = $location.search().oneId;
                $scope.currentt = $location.search().threeId;
                $scope.currenttwo = $location.search().twoId;
            }
        });

        var txtoemId = $("#txtOEM").val();
        //判断是导入还是手动添加
        if ($location.search().oneId || txtoemId != "") {
            //手动添加前是否导入过            
            if (localStorage.getItem('localData')) {
                //              console.log("aaa")
                //              console.log(localStorage.getItem('OEMARRTRUE'))
                if (localStorage.getItem('OEMARRTRUE')) {

                    var param = localStorage.getItem('OEMARRTRUE').split(",");
                    var getLocalData = localStorage.getItem('localData');
                    var jsonObj = JSON.parse(getLocalData);
                    var arr = [];
                    $.each(jsonObj, function(i, data) {

                            if (JSON.stringify(data.oemId) == "{}") {
                                //  console.log('没有oemId');
                            } else {

                                arr.push(data.oemId);
                            }
                        })
                        // console.log(param)
                        // console.log(arr)
                    for (var i = 0; i < param.length; i++) {
                        for (var j = i; j < arr.length; j++) {
                            if (param.indexOf(arr[j]) >= 0) {
                                param.splice(param.indexOf(arr[j]), 1);
                            }
                        }
                    }
                    localStorage.setItem('OEMARRTRUE', param);
                    var params = {
                            "oemId": param
                        }
                        //                  console.log(params)
                    commonService.inquirySheet(params, function(data) {
                        // console.log(data)
                        console.log(data.data)
                        for (var i = 0; i < jsonObj.length; i++) {

                            data.data.push(jsonObj[i])
                        }
                        $scope.jsonData = data.data;
                        //                      $scope.aa="品牌";
                    })
                } else {
                    var getLocalData = localStorage.getItem('localData');
                    var jsonObj = JSON.parse(getLocalData);
                    $scope.jsonData = jsonObj;
                }

            } else {
                //              console.log(localStorage.getItem('OEMARRTRUE'))
                if (localStorage.getItem('OEMARRTRUE')) {

                    var param = localStorage.getItem('OEMARRTRUE').split(",");
                    //  console.log(param)
                    var params = {
                            "oemId": param
                        }
                        //                  console.log(param)
                    commonService.inquirySheet(params, function(data) {
                        // console.log(data)
                        $scope.jsonData = data.data;
                        console.log(data.data)
                    })
                } else {
                    //                  return;
                }

            }


        } else {
            if (localStorage.getItem('OEMARRTRUE')) {
                var param = localStorage.getItem('OEMARRTRUE').split(",");
                var getLocalData = localStorage.getItem('localData');
                //console.log(getLocalData); // string  从localstorage本地缓存里面取得数据是json字符串类型,需要转化成json对象才可以用
                var jsonObj = JSON.parse(getLocalData); //将json字符串类型,转化成json对象，才可以调用里面的数据
                //              console.log(jsonObj);
                var arr = [];
                $.each(jsonObj, function(i, data) {

                    if (JSON.stringify(data.oemId) == "{}") {
                        console.log('没有oemId');
                    } else {

                        arr.push(data.oemId);
                    }
                })
                for (var i = 0; i < param.length; i++) {
                    for (var j = i; j < arr.length; j++) {
                        if (param.indexOf(arr[j]) >= 0) {
                            param.splice(param.indexOf(arr[j]), 1);
                        }
                    }
                }
                localStorage.setItem('OEMARRTRUE', param);
                var params = {
                    "oemId": param
                }
                console.log(params)
                commonService.inquirySheet(params, function(data) {
                    // console.log(data)

                    for (var i = 0; i < jsonObj.length; i++) {
                        data.data.push(jsonObj[i])
                    }
                    $scope.jsonData = data.data;
                })
            } else {
                var getLocalData = localStorage.getItem('localData');
                //console.log(getLocalData); // string  从localstorage本地缓存里面取得数据是json字符串类型,需要转化成json对象才可以用
                var jsonObj = JSON.parse(getLocalData); //将json字符串类型,转化成json对象，才可以调用里面的数据
                console.log(jsonObj);
                var arr = [];
                $.each(jsonObj, function(i, data) {

                    if (JSON.stringify(data.oemId) == "{}") {
                        console.log('没有oemId');
                    } else {

                        arr.push(data.oemId);
                    }
                })
                console.log(arr);
                $scope.jsonData = jsonObj;
            }

        }



        // $scope.arrDataJson = [];
        /*点击全选*/
        $scope.selectAll = false;
        $scope.allData = function(flag) {

            console.log(flag);
            if (flag == true) {
                $scope.selectCheck = function() {
                    return true;
                }
            } else {
                $scope.selectCheck = function() {
                    return false;
                }
            }

        }

        /*点击删除按钮，重新往本地缓存里面写数据*/
        $scope.del = function(n, data) {

            if (data.oemId) {

                if (localStorage.getItem('OEMARRTRUE')) {
                    var twoOEM = localStorage.getItem('OEMARRTRUE').split(",");
                    console.log(twoOEM)
                    if (twoOEM.indexOf(data.oemId) >= 0) {
                        $scope.jsonData.splice(twoOEM.indexOf(data.oemId), 1)
                        twoOEM.splice(twoOEM.indexOf(data.oemId), 1);
                        localStorage.setItem('OEMARRTRUE', twoOEM);
                    } else {
                        $scope.jsonData.splice(n, 1) //从第n个开始删除一个
                        var strData = JSON.stringify($scope.jsonData);
                        localStorage.setItem('localData', strData);
                    }
                } else {
                    $scope.jsonData.splice(n, 1) //从第n个开始删除一个
                    var strData = JSON.stringify($scope.jsonData);
                    localStorage.setItem('localData', strData);
                }


            } else {
                $scope.jsonData.splice(n, 1) //从第n个开始删除一个
                var strData = JSON.stringify($scope.jsonData);
                localStorage.setItem('localData', strData);
            }


        };

        /*点击全选之后删除本地数据*/
        $scope.allDel = function(e, jsonData) {
            var checkbox = $(e.target).closest('table').find('tbody input[type="checkbox"]');

            var j = 0;
            checkbox.each(function(i, v) {

                if (v.checked) {
                    $scope.jsonData.splice(i - j, 1);
                    j++;
                }
            });
            if (j == 0) {
                alert("没有选择需要删除的数据");
            } else {
                localStorage.removeItem('OEMARRTRUE');
                var strData1 = JSON.stringify($scope.jsonData);
                localStorage.setItem('localData', strData1);
            }

        };

        //返回二级页面
        $scope.back = function() {

            if ($location.search().oneId || txtoemId != "") {
                $state.go('main.selectCar', { threeId: $location.search().threeId, twoId: $location.search().twoId, oneId: $location.search().oneId, txtoemId: txtoemId, partsId: $location.search().partsId });
            } else {

                var getPartsId = localStorage.getItem('PartstId');
                var jsonObjPartsId = JSON.parse(getPartsId);
                //              console.log(jsonObjPartsId.partsId);
                var partsId = jsonObjPartsId.partsId;
                $state.go('main.selectCar', { partsId: partsId });
            }
        };

        //提交询价单
        $scope.button = function() {

            localStorage.removeItem('OEMARRTRUE');
            localStorage.removeItem('localData');
            window.location.href = "#/main.html";


        }
    }])


})
